package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.questionbank.QuestionStateType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 试题状态实体类
 * 对应数据库表：question_states
 *
 * 管理试题的当前状态信息，记录状态变更历史和操作人信息。
 * 支持试题状态流转的完整生命周期管理。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题状态表")
@SQLDelete(sql = "UPDATE question_states SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionState {

    /**
     * 状态记录唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 当前状态
     */
    @NotNull(message = "当前状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "current_state", nullable = false, length = 20)
    private QuestionStateType currentState;

    /**
     * 前一状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "previous_state", length = 20)
    private QuestionStateType previousState;

    /**
     * 状态变更原因
     */
    @Size(max = 200, message = "状态变更原因长度不能超过200个字符")
    @Column(name = "state_reason", length = 200)
    private String stateReason;

    /**
     * 操作人ID
     */
    @NotNull(message = "操作人ID不能为空")
    @Size(max = 50, message = "操作人ID长度不能超过50个字符")
    @Column(name = "operator_id", nullable = false, length = 50)
    private String operatorId;

    /**
     * 操作人姓名
     */
    @Size(max = 100, message = "操作人姓名长度不能超过100个字符")
    @Column(name = "operator_name", length = 100)
    private String operatorName;

    /**
     * 操作时间
     */
    @NotNull(message = "操作时间不能为空")
    @Column(name = "operation_time", nullable = false)
    private LocalDateTime operationTime;

    /**
     * 状态过期时间
     */
    @Column(name = "expiration_time")
    private LocalDateTime expirationTime;

    /**
     * 状态优先级
     */
    @Column(name = "priority")
    @Builder.Default
    private Integer priority = 0;

    /**
     * 状态描述
     */
    @Size(max = 500, message = "状态描述长度不能超过500个字符")
    @Column(name = "state_description", length = 500)
    private String stateDescription;

    /**
     * 状态标签
     */
    @Size(max = 200, message = "状态标签长度不能超过200个字符")
    @Column(name = "state_tags", length = 200)
    private String stateTags;

    /**
     * 状态元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

}